local assets =
{
    Asset("ANIM", "anim/woodlegs_cage_npc.zip"),
    Asset("ANIM", "anim/npc_item_woodlegs_cage_keys.zip"),
    Asset( "IMAGE", "images/inventoryimages/npc_item_woodlegs_cage_key1.tex" ),
    Asset( "ATLAS", "images/inventoryimages/npc_item_woodlegs_cage_key1.xml" ),
    Asset( "IMAGE", "images/inventoryimages/npc_item_woodlegs_cage_key2.tex" ),
    Asset( "ATLAS", "images/inventoryimages/npc_item_woodlegs_cage_key2.xml" ),
    Asset( "IMAGE", "images/inventoryimages/npc_item_woodlegs_cage_key3.tex" ),
    Asset( "ATLAS", "images/inventoryimages/npc_item_woodlegs_cage_key3.xml" ),
}
local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_item_woodlegs_cage"] or {}
end


local function unlock_task(inst)
    -- TheWorld:PushEvent("woodlegs_out_of_cage.task_end")
    inst.npc_base_lib:RPC_World_PushEvent("woodlegs_out_of_cage.task_end")
    
    if inst._talking_task then
        inst._talking_task:Cancel()
    end
    inst.AnimState:PlayAnimation("unlocked")
    inst:ListenForEvent("animover",function()
        inst.AnimState:ClearOverrideBuild("woodlegs_npc")
        local pt = Vector3(inst.Transform:GetWorldPosition())
        local npc = SpawnPrefab("woodlegs_npc")
        npc.Ready = true
        npc.AnimState:PlayAnimation("sleep", true)
        inst.npc_base_lib:RPC_PushEvent("RPC_Remove")
        inst:Remove()
        npc.npc_base_lib:Add2Child(inst)
        npc.Transform:SetPosition(pt.x,0, pt.z)
        npc.npc_base_lib:Try_2_Set_Light(true)
        npc:DoTaskInTime(0,function()
            -- npc.AnimState:PlayAnimation("wakeup")
            -- npc.AnimState:PushAnimation("idle",true)
            npc.sg:GoToState("wakeup")
        end)
        npc:DoTaskInTime(5,function()
            npc.npc_base_lib:Say(GetStringsTable().unlock_say)
            npc:DoTaskInTime(2,function()
                local theplayer = npc:GetNearestPlayer()
                if theplayer and theplayer.components.playercontroller then
                    npc:PushEvent("player_link",theplayer)
                    npc:AddDebuff("npc_event_woodlegs_out_of_cage", "npc_event_woodlegs_out_of_cage")
                else
                    npc:AddDebuff("npc_event_leave", "npc_event_leave")
                end
            end)
        end)
    end)
end



local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
	inst.entity:AddNetwork()
    inst.entity:AddSoundEmitter()
    inst.entity:AddDynamicShadow()
    inst.entity:AddLight()


    inst.entity:AddAnimState()
    inst.AnimState:SetBank("woodlegs_cage")
    inst.AnimState:SetBuild("woodlegs_cage")
    inst.AnimState:PlayAnimation("idle_swing",true)

    inst.DynamicShadow:SetSize(6, 2)
    MakeObstaclePhysics(inst, 2)

	inst:AddTag("npc_item_woodlegs_cage")

    inst.entity:SetPristine()

    inst.talk_sound = "dontstarve/characters/woodie/talk_LP"

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 35
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(0/255, 245/255, 255/255)
    inst.components.talker.offset = Vector3(0, -560, 0)
    inst.components.talker:MakeChatter()

    inst.Light:SetIntensity(.8)
    inst.Light:SetRadius(1.3)
    inst.Light:SetFalloff(.65)
    inst.Light:SetColour(255 / 255, 255 / 255, 236 / 255)
    inst.Light:Enable(true)

    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")
    inst:AddComponent("inspectable") --可检查组件
    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable().name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_woodlegs_cage")] = GetStringsTable().inspect_str  --人物检查的描述

    inst.AnimState:AddOverrideBuild("woodlegs_npc")
    -- inst.AnimState:Hide("KEY1")
    -- inst.AnimState:Hide("KEY2")
    -- inst.AnimState:Hide("KEY3")
    ---------------------------------------------------------------------------------------------------
    inst:AddComponent("trader") ----- 钥匙放置系统
        inst.components.trader.deleteitemonaccept = true
        local accept_test_fn = function(inst,item,...)
            if item and  item:HasTag("npc_item_woodlegs_cage_key") and item.keyName then
                return true
            end
            return false
        end
        inst.components.trader:SetAbleToAcceptTest(accept_test_fn)
        inst.components.trader:SetAcceptTest(accept_test_fn)
        inst.components.trader.onaccept = function(inst, giver, item)
            if item.keyName then
                inst.npc_everything_data:Set(item.keyName,true)
            end
            inst:PushEvent("unlock_check")
        end
    ---------------------------------------------------------------------------------------------------
    inst:DoTaskInTime(0,function()  --- 载入检查
        inst:PushEvent("unlock_check")
    end)
    inst:ListenForEvent("unlock_check",function()
        if inst.npc_everything_data:Get("key1") == true then
            inst.AnimState:Show("KEY1")
        else
            inst.AnimState:Hide("KEY1")
        end
        if inst.npc_everything_data:Get("key2") == true then
            inst.AnimState:Show("KEY2")
        else
            inst.AnimState:Hide("KEY2")
        end
        if inst.npc_everything_data:Get("key3") == true then
            inst.AnimState:Show("KEY3")
        else
            inst.AnimState:Hide("KEY3")
        end
        if inst.npc_everything_data:Get("key1") and inst.npc_everything_data:Get("key2") and inst.npc_everything_data:Get("key3") then
            inst:PushEvent("unlock")
        end        
    end)
    inst:ListenForEvent("unlock",unlock_task)
    ---------------------------------------------------------------------------------------------------
    inst._talking_task = inst:DoPeriodicTask(7,function()
        if inst:IsNearPlayer(10) then
            inst.npc_base_lib:Say(GetStringsTable().say,true)
        end
    end)
    return inst
end

local function key_common(keyName)
    local inst = CreateEntity()
    inst.keyName = keyName
    inst.entity:AddTransform()
	inst.entity:AddNetwork()
    inst.entity:AddAnimState()
    inst.AnimState:SetBank("npc_item_woodlegs_cage_keys")
    inst.AnimState:SetBuild("npc_item_woodlegs_cage_keys")
    inst.AnimState:PlayAnimation(keyName)
    inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")

    inst:AddTag("npc_item_woodlegs_cage_"..keyName)
    inst:AddTag("npc_item_woodlegs_cage_key")

	MakeInventoryPhysics(inst)
    MakeInventoryFloatable(inst, "med", nil, 0.75)

    inst.entity:SetPristine()
    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("tradable")
    inst:AddComponent("inspectable") --可检查组件    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.cangoincontainer = true
    inst.components.inventoryitem.imagename = "npc_item_woodlegs_cage_"..keyName
    inst.components.inventoryitem.atlasname = "images/inventoryimages/npc_item_woodlegs_cage_"..keyName..".xml"
    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable()[keyName])
    inst.components.inspectable:SetDescription(GetStringsTable()[keyName]..GetStringsTable().key_inspect_str)
    inst:DoTaskInTime(0.1,function()
        pcall(function()
            if TheWorld.npc_everything_data:Get("woodlegs_out_of_cage.task_end") == true then
                inst.components.inspectable:SetDescription(GetStringsTable()[keyName]..GetStringsTable().key_inspect_str_no_use)    
            end
        end)
    end)
    
    inst:ListenForEvent("on_landed",function()
        if inst:IsOnOcean(false) then
            inst.AnimState:PlayAnimation(inst.keyName.."_water",true)
        else
            inst.AnimState:PlayAnimation(inst.keyName)
        end
    end)
    inst:DoTaskInTime(0,function()
        inst:PushEvent("on_landed")
    end)

    return inst
end
local function key1()
    return key_common("key1")
end
local function key2()
    return key_common("key2")
end
local function key3()
    return key_common("key3")
end
return Prefab("npc_item_woodlegs_cage", fn,assets),
        Prefab("npc_item_woodlegs_cage_key1", key1,assets),
        Prefab("npc_item_woodlegs_cage_key2", key2,assets),
        Prefab("npc_item_woodlegs_cage_key3", key3,assets)